package com.star.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.pojo.Type;
import com.star.service.impl.TypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

/**
 * com.star.controller.admin
 *
 * @Description: 标签控制器
 * @Date: 2020-08-06 16:11
 * @Author: LENSTAR
 * @URL: http://123.57.161.114:9000/
 */
@Controller
@RequestMapping("/admin/types")
public class TypeController {

    @Autowired
    private TypeServiceImpl typeService;

    /** 分页查询分类列表
     * @param pageNum 页码
     */
    @GetMapping
    public String types(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        String orderBy = "id asc";//根据id排序
        PageHelper.startPage(pageNum, 4, orderBy);//分页规则
        List<Type> typeList = typeService.getAllType();//获取数据
        PageInfo<Type> pageInfo = new PageInfo<>(typeList);//数据分页后的详细信息
        model.addAttribute("pageInfo", pageInfo);//将信息传给前端
        return "admin/types";
    }

    @GetMapping("/input")
    public String input(Model model) {
        model.addAttribute("type",new Type());
        return "admin/types-input";
    }

    @PostMapping
    public String post(@Valid Type type, RedirectAttributes attributes) {
        Type type1 = typeService.getTypeByName(type.getName());
        if (type1 != null) {
            attributes.addFlashAttribute("message", "不能添加重复的分类!");
            return "redirect:/admin/types/input";
        }
        int save = typeService.save(type);
        if (save == 1) {//保存成功
            attributes.addFlashAttribute("message","新增成功!");
        }else {
            attributes.addFlashAttribute("message","新增失败!");
        }
        return "redirect:/admin/types";
    }

    @GetMapping("/{id}/input")
    public String editInput(@PathVariable("id") Long id, Model model) {
        model.addAttribute("type", typeService.getType(id));
        return "admin/types-input";
    }

    @PostMapping("/{id}")
    public String editPost(@Valid Type type,@PathVariable("id") Long id, RedirectAttributes attributes) {
        Type type1 = typeService.getTypeByName(type.getName());
        if (type1 != null) {
            attributes.addFlashAttribute("message", "不能添加重复的分类!");
            return "redirect:/admin/types/{id}/input";
        }
        int update = typeService.updateType(id, type);
        if (update == 1) {//更新成功
            attributes.addFlashAttribute("message","更新成功!");
        }else {
            attributes.addFlashAttribute("message","更新失败!");
        }
        return "redirect:/admin/types";
    }

    @GetMapping("/{id}/delete")
    public String delete(@PathVariable("id") Long id, RedirectAttributes attributes) {
        int delete = typeService.deleteType(id);
        if (delete == 1) {//删除成功
            attributes.addFlashAttribute("message","删除成功!");
        }else {
            attributes.addFlashAttribute("message","删除失败!");
        }
        return "redirect:/admin/types";
    }





}
